package com.woniuxy.carrental.service.impl;

import com.woniuxy.carrental.entity.*;
import com.woniuxy.carrental.mapper.ParkingMapper;
import com.woniuxy.carrental.mapper.RentCarInfoMapper;
import com.woniuxy.carrental.mapper.RentCarTypeMapper;
import com.woniuxy.carrental.mapper.WareingMapper;
import com.woniuxy.carrental.service.RenrCarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author author
 * @create 2021-10-02 21:59
 */

@Service
public class RenrCarInfoServiceImpl implements RenrCarInfoService {


    @Autowired
    RentCarInfoMapper rentCarInfoMapper;

    @Autowired
    ParkingMapper parkingMapper;

    @Autowired
    RentCarTypeMapper rentCarTypeMapper;

    @Autowired
    WareingMapper wareingMapper;



    @Override
    public List<RentCarInfo> selectAllRentCarInfo(RentCarInfo rentCarInfo, Integer pageIndex, Integer pageSize) {
        List<RentCarInfo> rentCarInfos = rentCarInfoMapper.selectAllRentCarInfo(rentCarInfo, pageIndex, pageSize);
        return rentCarInfos;
    }

    @Override
    @Transactional
    public void addRentCarInfo(RentCarInfo rentCarInfo) {
        rentCarInfo.setStoreid(2);
        rentCarInfo.setWaid(1);
        rentCarInfoMapper.addRentCarInfo(rentCarInfo);
        //修改停车场的数量
        int  parkid=rentCarInfo.getParkid();
        Parking parking = parkingMapper.selectByParkingid(parkid);
        int parkcarnumber =parking.getParkcarnumber()+1;
        parkingMapper.updateNumByParkid(parkid, parkcarnumber);
        //修改仓库的数量
        int car_typeid =rentCarInfo.getCar_typeid();
        RentCarType gettypebyid = rentCarTypeMapper.gettypebyid(car_typeid);
        int deid =gettypebyid.getDeid();
        Details details = rentCarInfoMapper.getdetailBydeid(deid);
        int decount=details.getDecount()-1;
        if(decount>=0){
            rentCarInfoMapper.updatedetailsnumBydeid(deid,decount);
        }else {
            throw new RuntimeException("系统维护中");
        }

        //自动生成出库订单
        Wareing wareing=new Wareing();
        wareing.setWareingcount(1);
        String uuid = CommonUtil.uuid();
        wareing.setWareingid(uuid);
        wareing.setWareingname(details.getDename());
        String data = DateUtil.data();
        wareing.setWareingdata(data);
        wareing.setWareingstate("出库");
        wareingMapper.addWareing(wareing);


    }

    @Override
    public void updateRentCarInfo(RentCarInfo rentCarInfo) {

         int rentcarid=rentCarInfo.getRentcarid();
        RentCarInfo rentCarInfoByrentcarid = rentCarInfoMapper.getRentCarInfoByrentcarid(rentcarid);
         int satrtparkid=rentCarInfoByrentcarid.getParkid();
         int endparkid=rentCarInfo.getParkid();
         int startparkcarnumber=parkingMapper.selectByParkingid(satrtparkid).getParkcarnumber()-1;
         int endparkcarnumber=parkingMapper.selectByParkingid(endparkid).getParkcarnumber()+1;
         parkingMapper.updateNumByParkid(satrtparkid,startparkcarnumber);
         parkingMapper.updateNumByParkid(endparkid, endparkcarnumber);
        rentCarInfoMapper.updateRentCarInfo(rentCarInfo);



    }

    @Override
    public int selectRentCarInfoCountBylimit(RentCarInfo rentCarInfo) {
        int i = rentCarInfoMapper.selectRentCarInfoCountBylimit(rentCarInfo);
        return i;
    }

    @Override
    @Transactional
    public void delectRentCarInfoCountByid(int rentcarid) {
        System.out.println(rentcarid);
        RentCarInfo rentCarInfo = rentCarInfoMapper.getRentCarInfoByrentcarid(rentcarid);
        System.out.println(rentCarInfo);
        int parkid =rentCarInfo.getParkid();
        Parking parking = parkingMapper.selectByParkingid(parkid);
        int parkcarnumber=parking.getParkcarnumber()-1;
        parkingMapper.updateNumByParkid(parkid, parkcarnumber);
        rentCarInfoMapper.delectRentCarInfoCountByid(rentcarid);

    }

    @Override
    public List<RentCarInfo> getallRentCarInfo() {
        return rentCarInfoMapper.getallRentCarInfo();
    }

    @Override
    public void updateStareByrentcarid(int rentcarid) {
        rentCarInfoMapper.updateStareByrentcarid(rentcarid);
    }

    @Override
    public RentCarInfo getRentCarInfoByCarnumber(String car_number) {
        return rentCarInfoMapper.selectRentCarInfoByCarNumber(car_number);
    }


}
